package com.moli.iov.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.base.cache.redis.VehicleBaseAuthCacheService;
import com.moli.iov.base.cache.redis.VehicleBaseCacheService;
import com.moli.iov.base.mapper.VehicleBaseAuthMapper;
import com.moli.iov.base.model.VehicleAttentionParam;
import com.moli.iov.base.model.VehicleBaseAuthParam;
import com.moli.iov.base.model.VehicleBaseParam;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.model.po.VehicleBaseAuth;
import com.moli.iov.base.service.IVehicleBaseAuthService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 车辆基础授权状态表 服务实现类
 * </p>
 *
 * @author wanghualin
 * @since 2020-02-19
 */
@Service
@Slf4j
public class VehicleBaseAuthServiceImpl extends ServiceImpl<VehicleBaseAuthMapper, VehicleBaseAuth> implements IVehicleBaseAuthService {
    @Autowired
    private VehicleBaseAuthCacheService authCacheService;

    @Override
    public RestResponse saveVehicleBaseAuth(VehicleBaseAuth baseAuth) {
        log.info("添加VehicleBaseAuth: " + baseAuth);
        if (baseAuth == null || StringUtils.isBlank(baseAuth.getVin())) {
            return RestResponse.failure("参数为空");
        }
        VehicleBaseAuth old = getVehicleBaseAuthDbByVin(baseAuth.getVin());
        if (old != null) {
            return RestResponse.failure("车辆已存在");
        }
        boolean save = this.save(baseAuth);

        authCacheService.setVehicleBaseAuth(baseAuth);
        return save ? RestResponse.success() : RestResponse.failure("添加失败");
    }

    @Override
    public VehicleBaseAuth getVehicleBaseAuthDbByVin(String vin) {
        VehicleBaseAuthParam param = new VehicleBaseAuthParam();
        param.queryEqVin(vin);
        return this.getOne(param.get(), false);
    }

    @Override
    public List<VehicleBaseAuth> listVehicleBaseAuth(){
        List<VehicleBaseAuth> list = authCacheService.listVehicleBaseAuth();
        if(list!=null){
            return list;
        }else{
            VehicleBaseAuthParam vehicleBaseParam = new VehicleBaseAuthParam();
            list = this.list(vehicleBaseParam.get());
            if (!CollectionUtils.isEmpty(list)) {
                authCacheService.saveBatchVehicleBaseAuth(list);
            }
            return list;
        }
    }

    @Override
    public VehicleBaseAuth getVehicleBaseAuthByVin(String vin) {
        if (StringUtils.isBlank(vin)) {
            throw new BusinessException("vin：参数为空");
        }
        VehicleBaseAuth auth = authCacheService.getVehicleBaseAuthByVin(vin);
        if (auth != null) {
            return auth;
        } else {
            VehicleBaseAuthParam authParam = new VehicleBaseAuthParam();
            authParam.queryEqVin(vin);
            return this.getOne(authParam.get(), false);
        }
    }

    @Override
    public RestResponse updateVehicleBaseAuth(VehicleBaseAuth baseAuth) {
        if (baseAuth == null || StringUtils.isBlank(baseAuth.getVin())) {
            throw new BusinessException("vin：参数为空");
        }
        VehicleBaseAuth auth = getById(baseAuth.getId());
        if (auth == null) {
            return RestResponse.failure("找不到修改的车辆");
        }
        //淘汰缓存
        authCacheService.deleteCompanyVehicleModel(auth);
        CommonUtils.copyProperties(baseAuth, auth);
        boolean save = this.saveOrUpdate(auth);
        if (save) {
            authCacheService.setVehicleBaseAuth(auth);
        }
        return save ? RestResponse.success() : RestResponse.failure("添加失败");

    }

    @Override
    public void initVehicleVehicleBaseAuth() {
        //清空缓存
        authCacheService.cleanVehicleBaseAuthCache();
        VehicleBaseAuthParam vehicleBaseParam = new VehicleBaseAuthParam();
        List<VehicleBaseAuth> list = this.list(vehicleBaseParam.get());
        if (!CollectionUtils.isEmpty(list)) {
            authCacheService.saveBatchVehicleBaseAuth(list);
        }
    }
}
